package com.xun.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xun.bean.User;
import com.xun.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.xun.controller
 * @ClassName: UserController
 * @Author: Think
 * @CreateTime: 2021/6/15 16:05
 * @Description:
 */
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    // 查询操作
    @GetMapping("/listUsers")
    public List<User> listUsers() {
        // SELECT user_id,user_name,age,user_email FROM zx_user
        return userMapper.selectList(null);
    }

    @GetMapping("/getUserById/{userId}")
    public List<User> getUserById(@PathVariable String userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        // SELECT user_id,user_name,age,user_email FROM zx_user WHERE (user_id = ?)
        queryWrapper.eq("user_id", userId);
        return userMapper.selectList(queryWrapper);
    }

    @GetMapping("/getUserByIds/{userIds}")
    public List<User> getUserByIds(@PathVariable String userIds) {
        List<String> userIdList = Arrays.asList(userIds.split(","));
        // SELECT user_id,user_name,age,user_email FROM zx_user WHERE user_id IN ( ? , ? , ? )
        return userMapper.selectBatchIds(userIdList);
    }

    @GetMapping("/getSingleUserById/{userId}")
    public User getSingleUserById(@PathVariable String userId) {
        // SELECT user_id,user_name,age,user_email FROM zx_user WHERE user_id=?
        return userMapper.selectById(userId);
    }

    @GetMapping("/getUserByName/{userName}")
    public List<User> getUserByName(@PathVariable String userName) {
        Map<String,Object> paramMap = new HashMap<>();
        // 还可以输入多个其它查询条件
        paramMap.put("user_name", userName);
        // SELECT user_id,user_name,age,user_email FROM zx_user WHERE user_name = ?
        return userMapper.selectByMap(paramMap);
    }

    @GetMapping("/countUsers/{userId}")
    public Integer countUsers(@PathVariable String userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        // SELECT COUNT( * ) FROM zx_user WHERE (user_id = ?)
        return userMapper.selectCount(queryWrapper);
    }

    @GetMapping("/getSingleUser/{userName}")
    public User getSingleUser(@PathVariable String userName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        // SELECT user_id,user_name,age,user_email FROM zx_user WHERE (user_id = ?)
        return userMapper.selectOne(queryWrapper);
    }

    // 新增操作
    @PostMapping("/addUser")
    public Integer addUser(@RequestBody User user){
        // INSERT INTO zx_user ( user_id, user_name, age, user_email ) VALUES ( ?, ?, ?, ? )
        return userMapper.insert(user);
    }

    // 删除操作
    @DeleteMapping("/deleteUserById/{userId}")
    public Integer deleteUserById(@PathVariable String userId){
        // DELETE FROM zx_user WHERE user_id=?
        return userMapper.deleteById(userId);
    }

    @DeleteMapping("/deleteBatchIds/{userIds}")
    public Integer deleteBatchIds(@PathVariable String userIds){
        List<String> userIdList = Arrays.asList(userIds.split(","));
        // DELETE FROM zx_user WHERE user_id IN ( ? , ? )
        return userMapper.deleteBatchIds(userIdList);
    }

    @DeleteMapping("/deleteByMap/{userName}")
    public Integer deleteByMap(@PathVariable String userName){
        Map<String,Object> paramMap = new HashMap<>();
        // 还可以输入多个其它查询条件
        paramMap.put("user_name", userName);
        // DELETE FROM zx_user WHERE user_name = ?
        return userMapper.deleteByMap(paramMap);
    }

    @DeleteMapping("/delete/{userId}")
    public Integer delete(@PathVariable String userId){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        // DELETE FROM zx_user WHERE (user_id = ?)
        return userMapper.delete(queryWrapper);
    }

    // 更新操作
    @PutMapping("/updateUserNameById")
    public Integer updateUserNameById(@RequestBody User user){
        // UPDATE zx_user SET user_name=? WHERE user_id=?
        return userMapper.updateById(user);
    }

    @PutMapping("/update/{userName}")
    public Integer update(@RequestBody User user, @PathVariable String userName){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        // user参数表示需要更新的内容，wrapper表示匹配记录的条件
        // 即使user参数中有userId，也不会去更新数据库的主键user_id
        // UPDATE zx_user SET user_name=?, age=? WHERE (user_name = ?)
        return userMapper.update(user, queryWrapper);
    }

    // 分页查询
    // 查询操作
    @GetMapping("/listUsersByPage/{pageNum}/{pageSize}")
    public List<User> listUsersByPage(@PathVariable Long pageNum, @PathVariable Long pageSize) {
        IPage<User> userPage = new Page<>(pageNum, pageSize);
        // SELECT COUNT(*) FROM zx_user WHERE deleted = 0
        // SELECT user_id,user_name,age,user_email,create_by,create_date,update_by,update_date,deleted FROM zx_user WHERE deleted=0 LIMIT ?,?
        return userMapper.selectPage(userPage, null).getRecords();
    }
}
